import validator from '@/common/validator'
import scroller from '@/components/scroller/scroller.vue';
import search from '@/components/search/search.vue';
import loading from '@/components/loading/loading.vue';
import pageLoading from '@/components/loading/pageLoading.vue';
import noData from '@/components/noData/noData.vue';
import iconfont from '@/components/iconfont/iconfont.vue';
export default {
	components: {
		search,
		pageLoading,
		loading,
		noData,
		scroller,
		iconfont
	},
	data() {
		return {
			current: null,
			showFlag1: 0,
			showFlag2: 0,
			// 返回的按钮是否显示
			isShow: false,
			// 返回顶部的标记点
			topItem: '',
			loadShow2: false,
			loadShow1: false,
			scroller: {},
			searchName: 'article-search',
			keyword: '',
			history: [],
			showHistory: true,
			provinceName: '',
			scroll_category_id: 'scroll_category_id_0',
			page_index: 1,
			page_size: 10,
			nav: [],
			slider: [],
			list: [],
			showNoData: false,
			showPageLoading: false,
			hasMoreData: true,
			showNavFloat: false,
			listTabs: [{
				name: '找公司',
			}, {
				name: '找招标',
			}],
			// 记录当前的搜索类型  默认为 0 搜索公司   1  搜索招标
			typeFlag: 0,
			allSize: null,

			// 记录总页数
			allPage: null,
			allPage2: null,
			status: 'loadmore',
			status2: null,
			keyword: null,
			// 记录页数
			page: 1,
			pageSize: 10,
			// 公司的数量
			allCompany: [],
			// 保存搜索记录
			recordList: null,
			recordCompanyList: null,
			resultFlag: 1,
			// 显示无数据
			noExitData: null,
			noExitData2: null,
			loading: false,
			companyId: null,
			// 判断当前是普通搜索还是全库搜索
			searchFlag: null,
			// 控制联想框出现
			textTip2: true
		}
	},
	onShow() {
		this.$initPageTitle(); //初始化页面标题
	},
	onLoad(e) {
		if (e.current) {
			this.typeFlag = e.current
		}
		if (e.provinceName) {
			this.provinceName = e.provinceName;
		}
		let history = uni.getStorageSync(this.searchName);
		if (Array.isArray(history)) {
			this.history = history;
		}
		if (e.name) {
			//对上个页面传来的值进行搜索
			// console.log("e.name",e.name)
			this.keyword = e.name
			this.checkCompany()
		}
		if (e.id) {
			//对上个页面传来的值进行搜索
			this.companyId = e.id
			this.checkCompany()
		}
	},
	beforeMount() {
		// 判断缓存内有无搜索记录
		this.getRecordCompany()
		this.recordList = uni.getStorageSync('recordListSearch');
		if (this.recordList) {
			this.showFlag1 = 1
		}
	},
	onReachBottom() {
		// console.log("-----------", this.page, this.allPage)

	},
	methods: {
		allSearch() {
			// console.log("this.keyword", this.keyword)
			if (!validator.checkNullValue(this.keyword) || !this.keyword) {
				return;
			}
			this.searchFlag = 1
			this.page = 1
			this.allCompany = []
			this.textTip2 = false
			// console.log("this.textTip2", this.textTip2)
			this.noExitData=null
			this.moreSearch()
		},
		moreKeyWord(value) {
			this.keyword = value
		},
		textTipChange(value) {
			this.textTip2 = value
		},
		moreSearch() {
			let param = {
				'page': this.page,
				"pageSize": this.pageSize
			}
			if (this.companyId) {
				param.companyCid = this.companyId

			}
			if (this.keyword) {
				param.companyName = this.keyword
			}
			this.status = 'loading';
			this.status2 = 'loading';
			this.$app.request({
				url: this.$baseApp.api.check.findAllMore,
				data: param,
				method: 'GET',
				dataType: 'json',
				success: res => {
					if (res.code == 200) {
						this.allPage = res.data.totalPages
						this.allSize = res.data.numberOfElements
						if (this.allPage <= 1 || res.data.content.length < 10) {
							this.status = 'nomore'
						}

						res.data.content.forEach((item) => {
							if (item.companyName.length > 16) {
								item.companyName = item.companyName.slice(0, 16) + '...'
							}
							if (item.companyRegLocation.length > 20) {
								item.companyRegLocation = item.companyRegLocation.slice(0, 20) +
									'...'
							}
							if (item.companyHistoryNames) {
								item.companyHistoryNames = this.joinStr(item.companyHistoryNames,
									this.keyword)
								item.companyHistoryNames = item.companyHistoryNames.split("$$$")
							}
							item.companyName = this.joinStr(item.companyName, this.keyword)
							item.companyRegLocation = this.joinStr(item.companyRegLocation, this
								.keyword)
							this.allCompany.push(item)
						})
						if (!this.allCompany || this.allCompany.length == 0) {
							this.resultFlag = null
							
							this.noExitData = 0
						} else {
							this.resultFlag = 1
						}


					} else {

						this.showTip(res.msg)

						
						this.noExitData = 0
					}
				},
				complete: res => {
					this.status2 = null
				

				}
			});
		},
		refreshArr() {
			// console.log(this.page_index, this.allPage2, "-----------------------")
			if (this.typeFlag == 0) {
				if (this.page >= this.allPage) {
					this.status = 'nomore';
					return
				}
				this.status = 'loading';
				this.page = ++this.page;
				setTimeout(() => {
					if (this.searchFlag == 1) {
						this.moreSearch()
					} else {
						this.checkCompany()
					}


					if (this.page >= this.allPage) this.status = 'nomore';
					else this.status = 'loading';
				}, 2000)
			}
			if (this.typeFlag == 1) {
				// console.log(this.page_index, this.allPage2, "-----------------------")
				if (this.page_index >= this.allPage2) {
					this.status = 'nomore';
					return
				}
				this.status = 'loading';
				this.page_index = ++this.page_index;
				setTimeout(() => {
					this.getData()
					if (this.page_index >= this.allPage2) this.status = 'nomore';
					else this.status = 'loading';
				}, 2000)
			}



		},
		// 清除搜索结果
		clearResult() {
			this.allCompany = []
			this.noExitData = 0
		},

		// 关键字标红
		joinStr(str, key) {
			var reg = new RegExp((`(${key})`), "gi");
			var replace = '<span style="color:#FD463E;font-weight:bold;">$1</span>';
			return str.replace(reg, replace);
		},


		clickTabs(item) {
			// console.log('item_____________', item);
			this.typeFlag = item.index
			this.showFlag2 = 0
			this.resultFlag = 1

			this.allCompany = []
			// if(this.typeFlag==0&&this.keyword){

			// 	this.checkCompany()
			// }


		},
		// 删除公司全部记录
		closeAll() {

			this.recordCompanyList = []
			uni.setStorageSync('historyName', this.recordCompanyList);
			this.showFlag2 = 0
		},
		// 删除某个公司记录
		closehistory(index) {
			this.recordCompanyList.splice(index, 1)
			uni.setStorageSync('historyName', this.recordCompanyList);
		},
		// 拿取存储在缓存的公司名称
		getRecordCompany() {
			this.recordCompanyList = uni.getStorageSync('historyName');
			if (this.recordCompanyList) {
				this.showFlag2 = 1
			}
		},

		// 清除所有搜索记录
		clearAllTag() {
			this.recordList = []
			uni.setStorageSync('recordListSearch', this.recordList);
			this.showFlag1 = 0
		},
		// 删除某个记录
		closeTag(index) {
			this.recordList.splice(index, 1)
			// console.log("++++++++++++", index)
			// 保存至缓存
			uni.setStorageSync('recordListSearch', this.recordList);
		},
		// 搜索记录关键字
		searchRecord(item) {
			// console.log("-----------------------------")
			this.keyword = item
			this.allCompany = []
			this.checkCompany()
		},
		// 提示框
		showTip(data) {
			// 判断字符长度，长度大于7，用show，小于用alter
			if (data.length >= 7) {

				this.$refs.uToast.show({
					message: data,
					type: 'success'
				})
			} else {

				this.$alert(data);
			}

		},
		// 搜索
		searchResult(value) {

			// console.log("==============", value)
			this.companyId = null
			this.keyword = value
			// 点击搜索  验证不为空后，调用接口!this.keyword||this.keyword==" "
			if (!validator.checkNullValue(this.keyword) || !this.keyword) {
				return;
			}
			let i = 0
			// 将搜索的关键字保存到缓存之中
			if (!this.recordList) {
				this.recordList = []

			} else {

				// 判断该内容是否重复
				for (let item of this.recordList) {
					if (item == value) {
						i = 1
					}
				}

			}
			if (i == 0) {
				this.recordList.push(value)
			}
			this.page = 1
			// console.log("__________________", this.recordList)
			uni.setStorageSync('recordListSearch', this.recordList);
			this.allCompany = []
			this.checkCompany()
		},
		// 查询企业
		checkCompany() {
			this.searchFlag = null
			this.loadShow1 = true
			this.resultFlag = null
			this.noExitData = null
			this.noExitData2 = null
			if (this.allCompany.length == 0) {
				this.loading = true
			}
			let param = {
				'page': this.page,
				"pageSize": this.pageSize
			}
			if (this.companyId) {
				param.companyCid = this.companyId

			}
			if (this.keyword) {
				param.companyName = this.keyword
			}
			this.status = 'loading';
			this.status2 = 'loading';
			this.$app.request({
				url: this.$baseApp.api.dicData.findAll,
				data: param,
				method: 'GET',
				dataType: 'json',
				success: res => {
					if (res.code == 200) {
						this.allPage = res.data.totalPages
						this.allSize = res.data.numberOfElements
						if (this.allPage <= 1 || res.data.content.length < 10) {
							this.status = 'nomore'
						}

						res.data.content.forEach((item) => {
							if (item.companyName.length > 16) {
								item.companyName = item.companyName.slice(0, 16) + '...'
							}
							if (item.companyRegLocation.length > 20) {
								item.companyRegLocation = item.companyRegLocation.slice(0, 20) +
									'...'
							}
							if (item.companyHistoryNames) {
								item.companyHistoryNames = this.joinStr(item.companyHistoryNames,
									this.keyword)
								item.companyHistoryNames = item.companyHistoryNames.split("$$$")
							}
							item.companyName = this.joinStr(item.companyName, this.keyword)
							item.companyRegLocation = this.joinStr(item.companyRegLocation, this
								.keyword)
							this.allCompany.push(item)
						})
						if (!this.allCompany || this.allCompany.length == 0) {
							this.resultFlag = 1
							this.noExitData = 1
						}


					} else {

						this.showTip(res.msg)
						this.resultFlag = 1
						this.noExitData = 1
					}
				},
				error:()=>{
						this.status = 'nomore'
				},
				complete: res => {
					this.loading = false
					this.loadShow1 = false
					this.status2 = null
				}
			});
		},
		/*搜索*/
		initSearch() {
			this.showHistory = true;
		},
		search(keyword) {
			if (keyword != '') {
				this.keyword = keyword;
			}
			this.showHistory = false;
			if (!this.history.includes(this.keyword)) {
				this.history.push(this.keyword);
				uni.setStorageSync(this.searchName, this.history);
			}
			this.nav = [];
			this.tabs = [];
			this.loadData();
		},

		/*加载数据*/
		loadData() {
			this.hasMoreData = true;
			this.page_index = 1;
			this.showNoData = false;
			this.slider = [];
			this.list = [];
			this.getData();
		},

		/*获取数据*/
		getData() {
			if (this.allCompany.length == 0) {
				this.loading = true
			}
			this.loadShow2 = true
			this.status = 'loading';
			let url = this.$baseApp.api.zhaobiao.zbList;
			this.$app.request({
				url: url,
				data: {
					projectName: this.keyword,
					provinceName: this.provinceName,
					page: this.page_index,
					pageSize: this.page_size
				},
				method: 'GET',
				dataType: 'json',
				success: res => {
					// console.info("查询的结果是", res)
					if (res.code == 200) {

						if (this.scroller.num == 1) {
							this.list = [];
						}
						this.showPageLoading = false;
						if (res.data.content.length == 0 || res.data.content.length < 10) {

							this.status = 'nomore'

						}
						if (res.data.content.length == 0) {
							this.showNoData = true;
							return;
						}

						this.allPage2 = res.data.totalPages
						let arr
						res.data.content.forEach(item => {
							if (item.noticeTitle.length >= 36) {
								arr = item.noticeTitle
								// console.log("item.noticeTitle=", typeof item.noticeTitle)
								item.noticeTitle = arr.slice(0, 35) + "..."
							}
							item.noticeTitle = this.joinStr(item.noticeTitle, this
								.keyword)
							item.areaName = this.joinStr(item.areaName, this
								.keyword)
							item.noticeType = this.joinStr(item.noticeType, this
								.keyword)


						})
						// console.log("++++++++++++++", res)
						this.list = this.list.concat(res.data.content);
						this.allSize = res.data.numberOfElements
						//this.scroller.endByPage(res.data.content.length, res.data.totalPages);

						uni.hideLoading();
					} else {
						this.scroller.endSuccess();
						this.$alert(res.msg);
					}
				},
				complete: res => {
					this.loading = false
					this.loadShow2 = false
					uni.stopPullDownRefresh();
					uni.hideLoading();
				}
			});
		},


		/*加载更多*/
		loadMore() {
			if (this.hasMoreData) {
				this.getData();
			}
		}


	}
}
